
package com.yc.class_attendance.utils;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.reflect.Type;

/**
 * Created by Administrator on 2016/12/28.
 */

public class MyFileUtils {//mResult

    public static String mets = Environment.getExternalStorageDirectory().getPath() + "/mResult/mets.json";
    public static String ps = Environment.getExternalStorageDirectory().getPath() + "/mResult/ps.json";

    public static String class_attemdamce = Environment.getExternalStorageDirectory().getPath() + "/mResult/class_attemdamce.json";
    public static String BigenCard = Environment.getExternalStorageDirectory().getPath() + "/mResult/BigenCard.json";
    public static String EndCard = Environment.getExternalStorageDirectory().getPath() + "/mResult/EndCard.json";
    public static String CourseId = Environment.getExternalStorageDirectory().getPath() + "/mResult/CourseId.json";


    public static String mResultPath = Environment.getExternalStorageDirectory().getPath() + "/mResult/mResult.json";
    public static String mListBean = Environment.getExternalStorageDirectory().getPath() + "/mResult/mListBean_kaoqin.json";

    public static String mListBeanB = Environment.getExternalStorageDirectory().getPath() + "/mResult/mListBean_kaoqinB.json";
    public static String mListAttachUrl = Environment.getExternalStorageDirectory().getPath() + "/mResult/mListAttachUrl.json";
    public static String funcPath = Environment.getExternalStorageDirectory().getPath() + "/func/func.json";
    public static String rolePath = Environment.getExternalStorageDirectory().getPath() + "/role/role.json";

    public static String classInfoPath = Environment.getExternalStorageDirectory().getPath() + "/classInfo/classInfo.json";
    public static String weekCoursePath = Environment.getExternalStorageDirectory().getPath() + "/weekCourse/weekCourse.json";
    public static String noticePath = Environment.getExternalStorageDirectory().getPath() + "/notice/notice.json";
    public static String picPath = Environment.getExternalStorageDirectory().getPath() + "/pic/pic.json";
    public static String videoPath = Environment.getExternalStorageDirectory().getPath() + "/video/video.json";

    public static boolean deleteDirectoty(String targetDirectoty) {

        File file = new File(targetDirectoty);
        if (!file.exists()) {
            return false;
        }
        File[] files = file.listFiles();
        boolean isSuccessful = false;
        if (file.length() == 0) {
            file.delete();
            return true;
        }
        //对所有列表的 路径进行判断 是否文件 还是文件夹
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                isSuccessful = deleteDirectoty(files[i].getAbsolutePath());

            } else if (files[i].isFile()) {
                isSuccessful = deleteFile(files[i].getAbsolutePath());

            }
            if (!isSuccessful) {//如果有删除失败的情况 直接跳出循环
                break;
            }
        }
        if (isSuccessful) {
            file.delete();
        }
        return isSuccessful;
    }

    public static boolean deleteFile(String path) {
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            if (!file.exists()) {
                return false;
            } else {
                try {
                    file.delete();
                    return true;
                } catch (Exception e) {
                    Log.e("ss", e.getMessage());
                    return false;
                }
            }
        } else {
            return false;

        }
    }

    public static <T> Object readFile2Object1(String path, Type typeOfT) {
        String json = readFile(path);
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        Gson gson = new Gson();
        return gson.fromJson(json, typeOfT);
    }

    public static <T> Object readFile2Object(String path, Class<T> mClass) {
        String json = readFile(path);
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        Gson gson = new Gson();
        return gson.fromJson(json, mClass);
    }

    public static String readFile(String path) {
        BufferedReader br = null;
        try {
            File e = new File(path);
            if (!e.exists()) {
                return null;
            }
            br = new BufferedReader(new FileReader(e));
            StringBuffer sb = new StringBuffer();
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            Log.d("ss", e.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    Log.d("ss", e.getMessage());
                }
            }
        }
        return null;
    }

    public static boolean deleteAndSaveObject2File(Object obj, String path) {
        boolean isSuccess = false;
        if (obj != null) {
            Gson gson = new Gson();
            String json = gson.toJson(obj);
            if (null != json) {
                PrintWriter printWriter = null;
                try {
                    File file = new File(path);
                    if (file.exists()) {
                        file.delete();
                        Log.d("ss", "json存在删除--");
                    }
                    File fileTemp = createFile(path);
                    printWriter = new PrintWriter(new BufferedWriter(new FileWriter(fileTemp)));
                    printWriter.print(json);
                    isSuccess = true;
                    Log.d("ss", "json存在保存--");

                } catch (Exception e) {
                    isSuccess = false;
                    Log.d("ss", e.getMessage());
                } finally {
                    if (printWriter != null) {
                        printWriter.close();
                    } else {
                        isSuccess = false;
                    }
                }
            }

        }
        return isSuccess;
    }

    public static boolean saveObject2File(Object obj, String path) {
        boolean isSuccess = false;
        if (obj != null) {
            Gson gson = new Gson();
            String json = gson.toJson(obj);
            if (null != json) {
                File file = createFile(path);
                PrintWriter printWriter = null;
                try {
                    printWriter = new PrintWriter(new BufferedWriter(new FileWriter(file)));
                    printWriter.print(json);
                    isSuccess = true;
                } catch (Exception e) {
                    isSuccess = false;
                    Log.d("ss", e.getMessage());
                } finally {
                    if (printWriter != null) {
                        printWriter.close();
                    } else {
                        isSuccess = false;
                    }
                }

            }

        }
        return isSuccess;
    }

    public static File createFile(String filePath) {
        File desFile = null;
        try {
            String desDir = filePath.substring(0, filePath.lastIndexOf(File.separator));
            File dir = new File(desDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            desFile = new File(filePath);
            if (!desFile.exists()) {
                desFile.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return desFile;
    }

}

